package co.fitstart.admin.entity.search;

import java.util.Date;
import java.util.Map;

import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

import co.fitstart.entity.credit.order.CreditOrderStatus;
import co.fitstart.entity.credit.product.CreditProductType;


public class CreditOrderSearch implements QueryBuilder{

    private String name;
    
    private String orderCode;

    private String mobile;
    
    private CreditProductType orderType;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTimeStart;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTimeEnd;
    
    private CreditOrderStatus status;
    
    private String productName;
    
    private Order order;

    private Filter filter;
    
    /**
     * default constructor
     */
    public CreditOrderSearch() {
        order = new Order("createTime", "DESC");
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {
        
        if(filter == null || refresh) {
            
            filter =  new Filter().addEquals("disabled", false)
                                  .addEquals("type", orderType)
                                  .addEquals("status", status)
                                  .addLike("code", orderCode)
                                  .addLike("user.name", name)
                                  .addLike("user.mobile", mobile)
                                  .addLike("product.name", productName)
                                  .addDateTimeBetween("createTime", createTimeStart, createTimeEnd);
        }
        
        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        return "FROM CreditOrder " + getPreparedFilter(false).getWhereSentence() + order.getSentence();
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }

	/**
	 * @return the name
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * @return the orderCode
	 */
	public String getOrderCode() {
		return this.orderCode;
	}

	/**
	 * @return the mobile
	 */
	public String getMobile() {
		return this.mobile;
	}

	/**
	 * @return the orderType
	 */
	public CreditProductType getOrderType() {
		return this.orderType;
	}

	/**
	 * @return the createTimeStart
	 */
	public Date getCreateTimeStart() {
		return this.createTimeStart;
	}

	/**
	 * @return the createTimeEnd
	 */
	public Date getCreateTimeEnd() {
		return this.createTimeEnd;
	}

	/**
	 * @return the status
	 */
	public CreditOrderStatus getOrderStatus() {
		return this.status;
	}

	/**
	 * @return the productName
	 */
	public String getProductName() {
		return this.productName;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param orderCode the orderCode to set
	 */
	public void setOrderCode(String orderCode) {
		this.orderCode = orderCode;
	}

	/**
	 * @param mobile the mobile to set
	 */
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	/**
	 * @param orderType the orderType to set
	 */
	public void setOrderType(CreditProductType orderType) {
		this.orderType = orderType;
	}

	/**
	 * @param createTimeStart the createTimeStart to set
	 */
	public void setCreateTimeStart(Date createTimeStart) {
		this.createTimeStart = createTimeStart;
	}

	/**
	 * @param createTimeEnd the createTimeEnd to set
	 */
	public void setCreateTimeEnd(Date createTimeEnd) {
		this.createTimeEnd = createTimeEnd;
	}

	/**
	 * @param orderStatus the status to set
	 */
	public void setOrderStatus(CreditOrderStatus status) {
		this.status = status;
	}

	/**
	 * @param productName the productName to set
	 */
	public void setProductName(String productName) {
		this.productName = productName;
	}
    
}
